package com.usian.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.SensitiveMapper;
import com.usian.admin.service.SensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.usian.common.exception.CatchCustomException;

import java.util.Arrays;
import java.util.Date;

/**
 * @创建人 lingyin
 * @创建时间 2022/10/10
 * @描述  敏感词业务层
 */
@Service
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, AdSensitive> implements SensitiveService {

    @Autowired
    SensitiveMapper sensitiveMapper;

    @Override
    /**
     *@描述  分页
     *@params [sensitiveDto]
     *@return com.usian.model.common.dtos.PageResponseResult
     */
    public PageResponseResult findAll(SensitiveDto sensitiveDto) {
        //判断对象为空
        if (sensitiveDto==null){
            CatchCustomException.catchs(20007,"参数不合法");
        }
        //初始化分页参数
        sensitiveDto.checkParam();
        //封装查询条件
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(sensitiveDto.getName()!=null&&!"".equals(sensitiveDto.getName()),
                AdSensitive::getSensitives,sensitiveDto.getName());
        //执行分页操作
        Page<AdSensitive> page = new Page<>(sensitiveDto.getPage(),sensitiveDto.getSize());
        IPage<AdSensitive> iPage = this.page(page,lambdaQueryWrapper);
        //封装分页结果
        PageResponseResult pageResponseResult=new PageResponseResult(iPage.getCurrent(),iPage.getSize(),iPage.getTotal());
        pageResponseResult.setData(iPage.getRecords());
        //返回结果
        return pageResponseResult;
    }

    @Override
    /**
     *@描述 添加
     *@params [adSensitive]
     *@return com.usian.model.common.dtos.ResponseResult
     */
    public ResponseResult add(AdSensitive adSensitive) {
        //为了代码健壮性 需要判断
        if(adSensitive == null){
            CatchCustomException.catchs(20007,"参数不合法");
        }
        if(adSensitive.getSensitives() == null) {
            CatchCustomException.catchs(20007,"参数不合法");
        }
        //维护字段
        adSensitive.setCreatedTime(new Date());
        //添加
        this.save(adSensitive);
        return ResponseResult.okResult(null);
    }

    @Override
    /**
     *@描述 回显
     *@params [id]
     *@return com.usian.model.common.dtos.ResponseResult
     */
    public ResponseResult findById(Integer id) {
        // 1. 检查参数
        if(id==null){
            CatchCustomException.catchs(20008,"Id不能为空");
        }
        //根据id查询
        AdSensitive adSensitive = this.getById(id);
        return ResponseResult.okResult(adSensitive);
    }

    @Override
    /**
     *@描述 修改
     *@params [adSensitive]
     *@return com.usian.model.common.dtos.ResponseResult
     */
    public ResponseResult update(AdSensitive adSensitive) {
        //检查参数
        if(adSensitive == null){
            CatchCustomException.catchs(20007,"参数不合法");
        }
        if(adSensitive.getSensitives() == null) {
            CatchCustomException.catchs(20007,"参数不合法");
        }
        //进行修改判断 查看是否有对象
        AdSensitive adSensitive1 =  this.getById(adSensitive.getId());
        if (adSensitive1==null){
            CatchCustomException.catchs(20007,"参数不合法");
        }
        //有对象  修改
        this.saveOrUpdate(adSensitive);
        return ResponseResult.okResult(null);
    }

    @Override
    /**
     *@描述 删除
     *@params [id]
     *@return com.usian.model.common.dtos.ResponseResult
     */
    public ResponseResult delete(Integer id) {
        // 1. 检查参数
        if(id==null){
            CatchCustomException.catchs(20008,"Id不能为空");
        }
        //进行删除
        this.removeById(id);
        return ResponseResult.okResult(null);
    }

    @Override
    /**
     *@描述  批量删除
     *@params [ids]
     *@return com.usian.model.common.dtos.ResponseResult
     */
    public ResponseResult deletes(Integer[] ids) {
        if (ids==null || ids.length<1){
            CatchCustomException.catchs(20008,"Ids不能为空");
        }
        this.removeByIds(Arrays.asList(ids));
        return ResponseResult.okResult(null);
    }

    /**
     *@描述  批量删除
     *@params [ids]
     *@return com.usian.model.common.dtos.ResponseResult
     */
    @Override
    public ResponseResult deletes2(Integer[] ids) {
        if (ids==null || ids.length<1){
            CatchCustomException.catchs(20008,"Ids不能为空");
        }
        sensitiveMapper.deletes2(ids);
        return ResponseResult.okResult(null);
    }
}
